package ciyuan.Dimension;

import ciyuan.Block.BlockRegistryManager;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.WorldGenRegistries;
import net.minecraft.world.gen.GenerationStage;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.OreFeatureConfig;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.world.BiomeLoadingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber
public class SpawnOre {

    private static ConfiguredFeature<?, ?> salt_ore = null;
    private static ConfiguredFeature<?, ?> lignite_ore = null;
    private static ConfiguredFeature<?, ?> pyrite_ore = null;
    private static ConfiguredFeature<?, ?> aluminium_ore = null;
    private static ConfiguredFeature<?, ?> quartz_ore = null;
    private static ConfiguredFeature<?, ?> copper_ore = null;
    private static ConfiguredFeature<?, ?> tin_ore = null;
    private static ConfiguredFeature<?, ?> anthracite_ore = null;
    private static ConfiguredFeature<?, ?> silicon_ore = null;

    @SubscribeEvent
    public static void addFeatureToBiomes(BiomeLoadingEvent event) {
        boolean biomeCriteria = !(new ResourceLocation("soul_sand_valley")).equals(event.getName());

        if ((new ResourceLocation("warped_forest")).equals(event.getName())) {
            biomeCriteria = false;
        }

        if (biomeCriteria) {
            event.getGeneration().getFeatures(GenerationStage.Decoration.UNDERGROUND_ORES).add(() -> {
                return salt_ore;
            });
            event.getGeneration().getFeatures(GenerationStage.Decoration.UNDERGROUND_ORES).add(() -> {
                return lignite_ore;
            });
            event.getGeneration().getFeatures(GenerationStage.Decoration.UNDERGROUND_ORES).add(() -> {
                return pyrite_ore;
            });
            event.getGeneration().getFeatures(GenerationStage.Decoration.UNDERGROUND_ORES).add(() -> {
                return aluminium_ore;
            });
            event.getGeneration().getFeatures(GenerationStage.Decoration.UNDERGROUND_ORES).add(() -> {
                return quartz_ore;
            });
            event.getGeneration().getFeatures(GenerationStage.Decoration.UNDERGROUND_ORES).add(() -> {
                return copper_ore;
            });
            event.getGeneration().getFeatures(GenerationStage.Decoration.UNDERGROUND_ORES).add(() -> {
                return tin_ore;
            });
            event.getGeneration().getFeatures(GenerationStage.Decoration.UNDERGROUND_ORES).add(() -> {
                return anthracite_ore;
            });
//            event.getGeneration().getFeatures(GenerationStage.Decoration.UNDERGROUND_ORES).add(() -> {
//                return silicon_ore;
//            });
        }
    }

    @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
    private static class FeatureRegisterHandler {

        @SubscribeEvent
        public static void registerFeature(RegistryEvent.Register<Feature<?>> event) {
            salt_ore = Feature.ORE.configured(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, BlockRegistryManager.SaltOre.get().defaultBlockState(), 9)).range(64).squared().count(20);
            lignite_ore = Feature.ORE.configured(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, BlockRegistryManager.LigniteOre.get().defaultBlockState(), 9)).range(64).squared().count(20);
            pyrite_ore = Feature.ORE.configured(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, BlockRegistryManager.PyriteOre.get().defaultBlockState(), 9)).range(64).squared().count(20);
            aluminium_ore = Feature.ORE.configured(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, BlockRegistryManager.AluminiumOre.get().defaultBlockState(), 9)).range(32).squared().count(2);
            quartz_ore = Feature.ORE.configured(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, BlockRegistryManager.QuartzOre.get().defaultBlockState(), 9)).range(32).squared().count(2);
            copper_ore = Feature.ORE.configured(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, BlockRegistryManager.CopperOre.get().defaultBlockState(), 9)).range(32).squared().count(2);
            tin_ore = Feature.ORE.configured(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, BlockRegistryManager.TinOre.get().defaultBlockState(), 9)).range(32).squared().count(2);
            anthracite_ore = Feature.ORE.configured(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, BlockRegistryManager.AnthraciteOre.get().defaultBlockState(), 9)).range(64).squared().count(20);
            silicon_ore = Feature.ORE.configured(new OreFeatureConfig(OreFeatureConfig.FillerBlockType.NATURAL_STONE, BlockRegistryManager.SiliconOre.get().defaultBlockState(), 9)).range(32).squared().count(8);
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"ore_salt", salt_ore);
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"lignite_ore", lignite_ore);
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"pyrite_ore", pyrite_ore);
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"aluminium_ore", aluminium_ore);
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"quartz_ore", quartz_ore);
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"copper_ore", copper_ore);
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"tin_ore", tin_ore);
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"anthracite_ore", anthracite_ore);
            Registry.register(WorldGenRegistries.CONFIGURED_FEATURE,"silicon_ore", silicon_ore);
        }
    }

}
